package com.sduept.nwld.test.relations;

import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class UpdateBreakerTripTime extends DBConnection {
	List<String> ids = new ArrayList<>();
	public void update() throws SQLException {
		log.info("start");
		 PreparedStatement pst = null;	
			try {
				String update_sql = "UPDATE `nwld_data_configure_db`.`t_cim_breaker` SET `last_tripatime`= ? , `last_tripbtime`= ? , `last_tripctime`= ? WHERE  `id`=?;";
				getConnection();
				pst = conn.prepareStatement(update_sql);
				conn.setAutoCommit(false);
				int i=0;
				for (String id : ids) {
					Date d1 = DateUtil.randomDate("2015-12-31", "2016-12-31");
					Date d2 = DateUtil.randomDate("2015-12-31", "2016-12-31");
					Date d3 = DateUtil.randomDate("2015-12-31", "2016-12-31");
					pst.setTimestamp(1, new Timestamp(d1.getTime()));
					pst.setTimestamp(2, new Timestamp(d2.getTime()));
					pst.setTimestamp(3, new Timestamp(d3.getTime()));
					pst.setString(4, id);
					pst.addBatch();
					i++;
				}
				pst.executeBatch();
				conn.commit();  
				pst.close();
				conn.close();
			} catch (NumberFormatException e) {

				e.printStackTrace();
			} 
			
	}
	

	

	public UpdateBreakerTripTime query() throws Exception {
		String getall = "select id from t_cim_breaker t where  vl_id in (select id from t_cim_voltagelevel t2 where  t2.baseVoltage in( select id from t_cim_basevoltage t where t.nominalVoltage > 499));";
		java.sql.Statement st = null;
		getConnection();
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(getall);
			while (rs.next()) {
				String id = rs.getString(1);
				ids.add(id);
			}
			rs.close();
		} finally {
			st.close();
			closeConnection();
		}
		return this;
	}

	public static void main(String[] args) {
		try {
			new UpdateBreakerTripTime().query().update();
			;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
